<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工工号" prop="accountNumber">
        <el-input
          v-model="queryParams.accountNumber"
          placeholder="请输入员工工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择部门ID" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="员工姓" prop="firstName">
        <el-input
          v-model="queryParams.firstName"
          placeholder="请输入员工姓"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工名" prop="lastName">
        <el-input
          v-model="queryParams.lastName"
          placeholder="请输入员工名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户姓名" prop="fullName">
        <el-input
          v-model="queryParams.fullName"
          placeholder="请输入用户姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactNumber">
        <el-input
          v-model="queryParams.contactNumber"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入用户邮箱"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指派" prop="designation">
        <el-input
          v-model="queryParams.designation"
          placeholder="请输入指派"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家庭住址" prop="baseArea">
        <el-input
          v-model="queryParams.baseArea"
          placeholder="请输入家庭住址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出生日期" prop="birthday">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择出生日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="合同类型" prop="contractType">
        <el-select v-model="queryParams.contractType" placeholder="请选择合同类型" clearable size="small">
          <el-option
            v-for="dict in contractTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="毕业院校" prop="graduateSchool">
        <el-input
          v-model="queryParams.graduateSchool"
          placeholder="请输入毕业院校"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最高学历" prop="highestEducation">
        <el-input
          v-model="queryParams.highestEducation"
          placeholder="请输入最高学历"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证书" prop="certificate">
        <el-input
          v-model="queryParams.certificate"
          placeholder="请输入证书"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作年限" prop="workingYear">
        <el-input
          v-model="queryParams.workingYear"
          placeholder="请输入工作年限"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同结束日期" prop="endContract">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.endContract"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择合同结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="交叉工资" prop="crossSalary">
        <el-input
          v-model="queryParams.crossSalary"
          placeholder="请输入交叉工资"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最低值" prop="deMinimis">
        <el-input
          v-model="queryParams.deMinimis"
          placeholder="请输入最低值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月薪" prop="monthlySalary">
        <el-input
          v-model="queryParams.monthlySalary"
          placeholder="请输入月薪"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="hmo_limit" prop="hmoLimit">
        <el-input
          v-model="queryParams.hmoLimit"
          placeholder="请输入hmo_limit"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择用户性别" clearable size="small">
          <el-option
            v-for="dict in sexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:employee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:employee:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:employee:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:employee:export']"
        >导出</el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="employeeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="employeeId" />
      <el-table-column label="员工工号" align="center" prop="accountNumber" />
      <el-table-column label="部门ID" align="center" prop="deptId" />
      <el-table-column label="员工姓" align="center" prop="firstName" />
      <el-table-column label="员工名" align="center" prop="lastName" />
      <el-table-column label="用户姓名" align="center" prop="fullName" />
      <el-table-column label="联系电话" align="center" prop="contactNumber" />
      <el-table-column label="用户邮箱" align="center" prop="email" />
      <el-table-column label="指派" align="center" prop="designation" />
      <el-table-column label="家庭住址" align="center" prop="baseArea" />
      <el-table-column label="出生日期" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同类型" align="center" prop="contractType" :formatter="contractTypeFormat" />
      <el-table-column label="毕业院校" align="center" prop="graduateSchool" />
      <el-table-column label="最高学历" align="center" prop="highestEducation" />
      <el-table-column label="证书" align="center" prop="certificate" />
      <el-table-column label="工作年限" align="center" prop="workingYear" />
      <el-table-column label="合同结束日期" align="center" prop="endContract" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endContract, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交叉工资" align="center" prop="crossSalary" />
      <el-table-column label="最低值" align="center" prop="deMinimis" />
      <el-table-column label="月薪" align="center" prop="monthlySalary" />
      <el-table-column label="hmo_limit" align="center" prop="hmoLimit" />
      <el-table-column label="用户性别" align="center" prop="sex" :formatter="sexFormat" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:employee:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:employee:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工档案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工工号" prop="accountNumber">
          <el-input v-model="form.accountNumber" placeholder="请输入员工工号" />
        </el-form-item>
        <el-form-item label="部门ID">
          <el-select v-model="form.deptId" placeholder="请选择部门ID">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="员工姓" prop="firstName">
          <el-input v-model="form.firstName" placeholder="请输入员工姓" />
        </el-form-item>
        <el-form-item label="员工名" prop="lastName">
          <el-input v-model="form.lastName" placeholder="请输入员工名" />
        </el-form-item>
        <el-form-item label="用户姓名" prop="fullName">
          <el-input v-model="form.fullName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactNumber">
          <el-input v-model="form.contactNumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="指派" prop="designation">
          <el-input v-model="form.designation" placeholder="请输入指派" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="baseArea">
          <el-input v-model="form.baseArea" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同类型">
          <el-select v-model="form.contractType" placeholder="请选择合同类型">
            <el-option
              v-for="dict in contractTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="毕业院校" prop="graduateSchool">
          <el-input v-model="form.graduateSchool" placeholder="请输入毕业院校" />
        </el-form-item>
        <el-form-item label="最高学历" prop="highestEducation">
          <el-input v-model="form.highestEducation" placeholder="请输入最高学历" />
        </el-form-item>
        <el-form-item label="证书" prop="certificate">
          <el-input v-model="form.certificate" placeholder="请输入证书" />
        </el-form-item>
        <el-form-item label="工作年限" prop="workingYear">
          <el-input v-model="form.workingYear" placeholder="请输入工作年限" />
        </el-form-item>
        <el-form-item label="合同结束日期" prop="endContract">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.endContract"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择合同结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="交叉工资" prop="crossSalary">
          <el-input v-model="form.crossSalary" placeholder="请输入交叉工资" />
        </el-form-item>
        <el-form-item label="最低值" prop="deMinimis">
          <el-input v-model="form.deMinimis" placeholder="请输入最低值" />
        </el-form-item>
        <el-form-item label="月薪" prop="monthlySalary">
          <el-input v-model="form.monthlySalary" placeholder="请输入月薪" />
        </el-form-item>
        <el-form-item label="hmo_limit" prop="hmoLimit">
          <el-input v-model="form.hmoLimit" placeholder="请输入hmo_limit" />
        </el-form-item>
        <el-form-item label="用户性别">
          <el-radio-group v-model="form.sex">
            <el-radio
              v-for="dict in sexOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmployee, getEmployee, delEmployee, addEmployee, updateEmployee, exportEmployee } from "@/api/business/employee";

export default {
  name: "Employee",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工档案表格数据
      employeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 合同类型字典
      contractTypeOptions: [],
      // 用户性别字典
      sexOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accountNumber: null,
        deptId: null,
        firstName: null,
        lastName: null,
        fullName: null,
        contactNumber: null,
        email: null,
        designation: null,
        baseArea: null,
        birthday: null,
        contractType: null,
        graduateSchool: null,
        highestEducation: null,
        certificate: null,
        workingYear: null,
        endContract: null,
        crossSalary: null,
        deMinimis: null,
        monthlySalary: null,
        hmoLimit: null,
        sex: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        accountNumber: [
          { required: true, message: "员工工号不能为空", trigger: "blur" }
        ],
        fullName: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("contract_type").then(response => {
      this.contractTypeOptions = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
  },
  methods: {
    /** 查询员工档案列表 */
    getList() {
      this.loading = true;
      listEmployee(this.queryParams).then(response => {
        this.employeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 合同类型字典翻译
    contractTypeFormat(row, column) {
      return this.selectDictLabel(this.contractTypeOptions, row.contractType);
    },
    // 用户性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        employeeId: null,
        accountNumber: null,
        deptId: null,
        firstName: null,
        lastName: null,
        fullName: null,
        contactNumber: null,
        email: null,
        designation: null,
        baseArea: null,
        birthday: null,
        contractType: null,
        graduateSchool: null,
        highestEducation: null,
        certificate: null,
        workingYear: null,
        endContract: null,
        crossSalary: null,
        deMinimis: null,
        monthlySalary: null,
        hmoLimit: null,
        sex: "0",
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.employeeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工档案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const employeeId = row.employeeId || this.ids
      getEmployee(employeeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工档案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.employeeId != null) {
            updateEmployee(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addEmployee(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const employeeIds = row.employeeId || this.ids;
      this.$confirm('是否确认删除员工档案编号为"' + employeeIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEmployee(employeeIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有员工档案数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportEmployee(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
